﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using Microsoft.Win32;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace GW2_Marketeer__WPF_.Commands
{
    public class LoadGWSpidyDataCommand : ICommand
    {
        public LoadGWSpidyDataCommand()
        {
            Marketeer.Instance.PropertyChanged += (sender, e) => { if (e.PropertyName == "GWItems") notifyExecuteChanged(null); };
            Marketeer.Instance.PropertyChanged += (sender, e) => { if (e.PropertyName == "Loading") notifyExecuteChanged(null); };
        }
        
        public bool CanExecute(object parameter)
        {
            return Marketeer.Instance.GWItems.Any() && !Marketeer.Instance.Loading;
        }

        public event EventHandler CanExecuteChanged;
        private void notifyExecuteChanged(EventArgs e)
        {
            Application.Current.Dispatcher.Invoke(new Action(() => CanExecuteChanged(this, e ?? new EventArgs())));
        }

        private void doWork(object parameter)
        {
            Marketeer.Instance.ImportGWSpiderData();
        }

        public void Execute(object parameter)
        {
            var dialogTask = Task.Factory.StartNew(() => doWork(parameter));
        }
    }
}
